package org.popupcommunication.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.popupcommunication.model.Message;
import org.popupcommunication.service.MessageManager;
import org.popupcommunication.service.UserManager;
import org.popupcommunication.utils.Email;
import org.popupcommunication.web.UserFormRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("public/inscription")
public class RegisterController {
	@Autowired UserManager userService;
	@Autowired MessageManager messageService;
	
	private static final String form_register ="register/inscription";
	private static final String form_confirm ="users/confirmation";	
	
	@RequestMapping(value = "",method = RequestMethod.GET)
	public String showForm(Model model) {	
		model.addAttribute("utilisateur", new UserFormRegister());
		return form_register;
	}

	@RequestMapping(value="",method = RequestMethod.POST)
	public String postForm(HttpServletRequest request, Model model,
			@ModelAttribute("utilisateur")  @Valid UserFormRegister form, BindingResult result) throws FileNotFoundException {		
		Message msgDao=null;
		verifyConfirmationDatas(form,result);
		if (result.hasErrors())
			return form_register;		
		userService.register(form, result);
		//envoit emails
		try {
			String sujet = "Nouvelle inscription";
			msgDao = messageService.findByTitle("email_nouvelle_inscription");
			String msg = msgDao.getMessage();
			InputStream input = RegisterController.class.getClassLoader().getResourceAsStream("popup.properties");
			Properties prop = new Properties();	
			prop.load(input);	
			String link="<p><a href='"+prop.getProperty("popup.url")+"admin/'>Administration popup</a></p>";
			msg +=link;		
			Email.sendAdminEmail(request.getSession().getServletContext(),sujet,msg);
			msgDao = messageService.findByTitle("confirmation_inscription");
			Email.sendUserEmail(request.getSession().getServletContext(),"Popup confirmation inscription",msgDao.getMessage(),form.getEmail());
		} catch (IOException e) {
			e.printStackTrace();
		}
		model.addAttribute("user", form);
		model.addAttribute("msg", msgDao.getMessage());
		return  form_confirm;
	}
	
	private static void verifyConfirmationDatas(UserFormRegister u,BindingResult result){
		if (!u.getEmail().equals(u.getConfirmEmail())){
			if(!result.hasFieldErrors("email")){
				result.rejectValue("email", "error.account.email.mismatch.message");
			}		
			if(!result.hasFieldErrors("confirmEmail")){
				result.rejectValue("confirmEmail", "error.account.email.mismatch.message");
			}				
		}
		if(!u.getPassword().equals(u.getConfirmPassword())){
			if(!result.hasFieldErrors("password")){
				result.rejectValue("password", "error.account.password.mismatch.message");
			}		
			if(!result.hasFieldErrors("confirmPassword")){
				result.rejectValue("confirmPassword", "error.account.password.mismatch.message");
			}				
		}
	}
	
}
